using System;
using System.Drawing;
using System.Windows.Forms;

namespace GomokuGame
{
    /// <summary>
    /// 棋盘控件
    /// </summary>
    public class BoardControl : Control
    {
        private const int BOARD_SIZE = 15;
        private const int GRID_SIZE = 40;
        private const int BOARD_MARGIN = 20;
        private const int PIECE_RADIUS = 18;
        
        private GameEngine gameEngine;
        private bool isMouseDown = false;
        private Point lastMousePosition;

        /// <summary>
        /// 棋盘大小
        /// </summary>
        public int BoardSize => BOARD_SIZE;

        /// <summary>
        /// 应用设置到棋盘控件
        /// </summary>
        /// <param name="settings">游戏设置</param>
        public void ApplySettings(GameSettings settings)
        {
            // 这里可以应用颜色、样式等设置
            // 暂时只重新绘制
            Invalidate();
        }

        /// <summary>
        /// 网格大小
        /// </summary>
        public int GridSize => GRID_SIZE;

        /// <summary>
        /// 棋子落子事件
        /// </summary>
        public event EventHandler<BoardClickEventArgs> BoardClick;

        public BoardControl()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
            this.Size = new Size(BOARD_SIZE * GRID_SIZE + 2 * BOARD_MARGIN, BOARD_SIZE * GRID_SIZE + 2 * BOARD_MARGIN);
            this.BackColor = Color.BurlyWood;
        }

        /// <summary>
        /// 设置游戏引擎
        /// </summary>
        /// <param name="engine">游戏引擎</param>
        public void SetGameEngine(GameEngine engine)
        {
            gameEngine = engine;
            if (gameEngine != null)
            {
                gameEngine.PiecePlaced += OnPiecePlaced;
                gameEngine.PieceRemoved += OnPieceRemoved;
                gameEngine.GameStarted += OnGameStarted;
                gameEngine.GameEnded += OnGameEnded;
            }
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            // 绘制棋盘背景
            DrawBoard(g);

            // 绘制网格线
            DrawGrid(g);

            // 绘制棋子
            DrawPieces(g);

            // 绘制最后落子标记
            DrawLastMove(g);
        }

        /// <summary>
        /// 绘制棋盘背景
        /// </summary>
        /// <param name="g">图形对象</param>
        private void DrawBoard(Graphics g)
        {
            Rectangle boardRect = new Rectangle(BOARD_MARGIN, BOARD_MARGIN, 
                BOARD_SIZE * GRID_SIZE, BOARD_SIZE * GRID_SIZE);
            
            using (SolidBrush brush = new SolidBrush(Color.BurlyWood))
            {
                g.FillRectangle(brush, boardRect);
            }
        }

        /// <summary>
        /// 绘制网格线
        /// </summary>
        /// <param name="g">图形对象</param>
        private void DrawGrid(Graphics g)
        {
            using (Pen pen = new Pen(Color.Black, 1))
            {
                // 绘制横线
                for (int i = 0; i < BOARD_SIZE; i++)
                {
                    int y = BOARD_MARGIN + i * GRID_SIZE;
                    g.DrawLine(pen, BOARD_MARGIN, y, BOARD_MARGIN + (BOARD_SIZE - 1) * GRID_SIZE, y);
                }

                // 绘制竖线
                for (int i = 0; i < BOARD_SIZE; i++)
                {
                    int x = BOARD_MARGIN + i * GRID_SIZE;
                    g.DrawLine(pen, x, BOARD_MARGIN, x, BOARD_MARGIN + (BOARD_SIZE - 1) * GRID_SIZE);
                }
            }
        }

        /// <summary>
        /// 绘制棋子
        /// </summary>
        /// <param name="g">图形对象</param>
        private void DrawPieces(Graphics g)
        {
            if (gameEngine == null) return;

            for (int row = 0; row < BOARD_SIZE; row++)
            {
                for (int col = 0; col < BOARD_SIZE; col++)
                {
                    PieceType pieceType = gameEngine.GetPiece(row, col);
                    if (pieceType != PieceType.Empty)
                    {
                        DrawPiece(g, row, col, pieceType);
                    }
                }
            }
        }

        /// <summary>
        /// 绘制单个棋子
        /// </summary>
        /// <param name="g">图形对象</param>
        /// <param name="row">行坐标</param>
        /// <param name="col">列坐标</param>
        /// <param name="pieceType">棋子类型</param>
        private void DrawPiece(Graphics g, int row, int col, PieceType pieceType)
        {
            int x = BOARD_MARGIN + col * GRID_SIZE;
            int y = BOARD_MARGIN + row * GRID_SIZE;
            Rectangle rect = new Rectangle(x - PIECE_RADIUS, y - PIECE_RADIUS, 
                PIECE_RADIUS * 2, PIECE_RADIUS * 2);

            Color pieceColor = pieceType == PieceType.Black ? Color.Black : Color.White;
            Color borderColor = pieceType == PieceType.Black ? Color.Black : Color.Gray;

            // 绘制棋子阴影
            using (SolidBrush shadowBrush = new SolidBrush(Color.FromArgb(50, Color.Black)))
            {
                g.FillEllipse(shadowBrush, rect.X + 2, rect.Y + 2, rect.Width, rect.Height);
            }

            // 绘制棋子主体
            using (SolidBrush brush = new SolidBrush(pieceColor))
            {
                g.FillEllipse(brush, rect);
            }

            // 绘制棋子边框
            using (Pen pen = new Pen(borderColor, 1))
            {
                g.DrawEllipse(pen, rect);
            }

            // 为白子添加高光效果
            if (pieceType == PieceType.White)
            {
                using (SolidBrush highlightBrush = new SolidBrush(Color.FromArgb(100, Color.White)))
                {
                    Rectangle highlightRect = new Rectangle(x - PIECE_RADIUS / 2, y - PIECE_RADIUS / 2, 
                        PIECE_RADIUS, PIECE_RADIUS);
                    g.FillEllipse(highlightBrush, highlightRect);
                }
            }
        }

        /// <summary>
        /// 绘制最后落子标记
        /// </summary>
        /// <param name="g">图形对象</param>
        private void DrawLastMove(Graphics g)
        {
            if (gameEngine == null || gameEngine.MoveHistory.Count == 0) return;

            Move lastMove = gameEngine.MoveHistory[gameEngine.MoveHistory.Count - 1];
            int x = BOARD_MARGIN + lastMove.Col * GRID_SIZE;
            int y = BOARD_MARGIN + lastMove.Row * GRID_SIZE;

            using (Pen pen = new Pen(Color.Red, 2))
            {
                g.DrawEllipse(pen, x - 5, y - 5, 10, 10);
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (gameEngine == null || gameEngine.CurrentState != GameState.Playing) return;

            isMouseDown = true;
            lastMousePosition = e.Location;
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (!isMouseDown) return;

            isMouseDown = false;

            // 检查是否在棋盘范围内
            if (e.X < BOARD_MARGIN || e.X > BOARD_MARGIN + BOARD_SIZE * GRID_SIZE ||
                e.Y < BOARD_MARGIN || e.Y > BOARD_MARGIN + BOARD_SIZE * GRID_SIZE)
                return;

            // 计算网格坐标
            int col = (e.X - BOARD_MARGIN + GRID_SIZE / 2) / GRID_SIZE;
            int row = (e.Y - BOARD_MARGIN + GRID_SIZE / 2) / GRID_SIZE;

            if (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE)
            {
                BoardClick?.Invoke(this, new BoardClickEventArgs { Row = row, Col = col });
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (isMouseDown)
            {
                // 可以在这里添加鼠标拖拽效果
            }
        }

        /// <summary>
        /// 棋子落子事件处理
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件参数</param>
        private void OnPiecePlaced(object sender, MoveEventArgs e)
        {
            Invalidate();
        }

        /// <summary>
        /// 棋子移除事件处理
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件参数</param>
        private void OnPieceRemoved(object sender, MoveEventArgs e)
        {
            Invalidate();
        }

        /// <summary>
        /// 游戏开始事件处理
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件参数</param>
        private void OnGameStarted(object sender, GameEventArgs e)
        {
            Invalidate();
        }

        /// <summary>
        /// 游戏结束事件处理
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件参数</param>
        private void OnGameEnded(object sender, GameEventArgs e)
        {
            Invalidate();
        }
    }

    /// <summary>
    /// 棋盘点击事件参数
    /// </summary>
    public class BoardClickEventArgs : EventArgs
    {
        public int Row { get; set; }
        public int Col { get; set; }
    }
} 